<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Appoint;
use App\Models\HoneyBalance;
use App\Services\HoneyBalanceService;
use Dingo\Api\Http\Request;

class AppointController extends BaseController
{
    public $model = Appoint::class;


    public function index(Request $request) {
        $type = $request->input('type');
        $order_sn = $request->input('order_sn');
        $service_merchant = $request->input('service_merchant');
        $title = $request->input('title');
        $status = $request->input('status', -1);
        $phone = $request->input('phone');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $category = $request->input('category');

        $list = Appoint::withCertain('service', ['category', 'title', 'image_file', 'video_file', 'audio_file','service_merchant','category'])
            ->withCertain('user', ['nickname'])
            ->with(['comment'])
            ->startAt($start_at)
            ->endAt($end_at)->type($type)
            ->status($status)
            ->orderSn($order_sn)
            ->phone($phone)
            ->when($service_merchant, function ($q) use($service_merchant) {
                $q->whereHas('service', function ($qt) use($service_merchant){
                    $qt->where('service_merchant', 'like', '%' . $service_merchant . '%');
                });
            })
            ->when($title, function ($q) use($title) {
                $q->whereHas('service', function ($qt) use($title){
                    $qt->where('title', 'like', '%' . $title . '%');
                });
            })
            ->when($category, function ($q) use($category){
                if ($category == 1) {
                    $q->whereIn('category', [1, -1]);
                } else {
                    $q->where('category', $category);
                }
            })
            ->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));

        //获取已付总金额
        $extra['paid_amount'] = Appoint::getPaidAmount();
        return $this->paginate($list, $extra);
    }

    public function edit_price(Request $request) {
        $this->validate($request, [
            'id' => 'required',
            'amount' => 'required'
        ]);
        $input = $request->input();
        $appoint = Appoint::find($input['id']);
        if (!in_array($appoint->status, [Appoint::STATUS_WAIT_EDIT_PRICE, Appoint::STATUS_WAIT_PAY])) {
            return $this->failed('当前状态下不支持修改价格');
        }
        $appoint->amount = $input['amount'] * 100;
        $appoint->status = Appoint::STATUS_WAIT_PAY;
        $appoint->save();
        return $this->success('修改成功', ['id' => $input['id']]);
    }

    public function offline_pay(Request $request) {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $input = $request->input();

        return \DB::transaction(function () use($input){
            $arr_id = explode(",", $input['ids']);
            foreach ($arr_id as $id) {
                $appoint = Appoint::find($id);
                if (!in_array($appoint->status, [Appoint::STATUS_WAIT_PAY])) {
                    return $this->failed('当前状态下不支持支付');
                }
                $appoint->pay_type = Appoint::PAY_TYPE_OFFLINE;
                $appoint->status = Appoint::STATUS_PAID;
                $appoint->admin_id = $this->getUserId();
                if ($appoint->save()) {
                    HoneyBalanceService::store($appoint->user_id, HoneyBalance::POINT_APPOINT_PAY, HoneyBalance::TYPE_APPOINT, $appoint->id, false);
                }
            }
            return $this->success([],'支付成功');
        });
    }


}

